package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.example.demo.algorithm.BRBInference3;
import com.example.demo.mapper.StomachMapper;
import com.example.demo.pojo.*;
import com.example.demo.util.ReadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BRBService {

    @Autowired
    ReadUtil readUtil;

    @Autowired
    StomachMapper stomachMapper;

    private ArrayList<ArrayList<Double>> reference;
    private ArrayList<ArrayList<Double>> belief;

    public void Init() {
        Map map = readUtil.dataRead();
        reference = (ArrayList<ArrayList<Double>>) map.get("reference");
        belief = (ArrayList<ArrayList<Double>>) map.get("belief");
    }

    public FidOutDto CalculateRes(String RecId,
                                  String DeptId,
                                  String DataIn) {
        Init();
        List<DataInDto> dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        ArrayList<Double> input = new ArrayList<>();
        for (int i = 0; i < dataInDtos.size(); i++) {
            input.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        ArrayList<ArrayList<Double>> Input = new ArrayList<>();
        Input.add(input);


        BRBInference3 brbInference3 = new BRBInference3(reference, belief, Input);
        brbInference3.Init();
        brbInference3.Process();
        brbInference3.fusion();

        ArrayList<ArrayList<Double>> activation_weight = brbInference3.getActivation_weight();
        ArrayList<ArrayList<Double>> m_xin = brbInference3.getM_xin();

        List<ActivedRulesDto> ActivedRules = new ArrayList<>();
        for(int i=0; i<activation_weight.get(0).size(); i++){
            ActivedRulesDto temp = new ActivedRulesDto(i + 1, activation_weight.get(0).get(i));
            ActivedRules.add(temp);
        }

        List<DataOutDto> Result = new ArrayList<>();
        for(int i=0; i<m_xin.get(0).size(); i++){
            if(i<9){
                DataOutDto temp = new DataOutDto("sk0" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            } else {
                DataOutDto temp = new DataOutDto("sk" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            }
        }
        FidOutDto fidOutDto = new FidOutDto();
        fidOutDto.setActivedRules(ActivedRules);
        fidOutDto.setResult(Result);
        fidOutDto.setDeptId(DeptId);
        fidOutDto.setRecId(RecId);
        return fidOutDto;

    }

    public String updateService(String newRule){
        Init();
        List<Rule> rules = JSON.parseArray(newRule, Rule.class);
        Rule rule = rules.get(0);

        ArrayList<Double> single =new ArrayList<>();
        single.add(rule.getId01());
        single.add(rule.getId02());
        single.add(rule.getId03());
        single.add(rule.getId04());
        single.add(rule.getId05());
        single.add(rule.getId06());
        single.add(rule.getId07());
        single.add(rule.getId08());
        single.add(rule.getId09());

        int index = -1;
        boolean flag = true;
        for(int i=0; i<reference.size()&&flag; i++){
            int count = 0;
            for(int j=0; j<reference.get(i).size(); j++){
                if(Math.abs(single.get(j) - reference.get(i).get(j)) < 0.001 ){
                    count += 1;
                    if(count == single.size()) {
                        index = i;
                        flag = false;
                    }
                }
            }
        }

        if(index == -1){
            rule.setRule(reference.size()+1);
            stomachMapper.InsertSingleRule(rule);
            return "已添加第"+rule.getRule()+"条规则";
        }else {
            rule.setRule(index+1);
            stomachMapper.updateRule(rule);
            return "已覆盖第"+rule.getRule()+"条规则";
        }

    }

    public String deleteRule(int id){
        Init();
        if(id > reference.size()){
            return "输入的规则序号超出引索范围";
        }else {
            stomachMapper.deleteRule(id);
            stomachMapper.addSelf(id);
            return "已删除第"+id+"条规则";
        }
    }

    public ArrayList<Rule> showRules(){
        return stomachMapper.getAllData();
    }

}
